<!-- build time:Tue Apr 28 2020 09:17:49 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html class="theme-next pisces use-motion" lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="theme-color" content="#222"><script src="//cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script><link href="//cdn.jsdelivr.net/npm/pace-js@1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"><link href="https://fonts.loli.net/css?family=EB+Garamond:400,400i,700,700i|Noto+Serif+SC:400,500,700&display=swap&subset=chinese-simplified" rel="stylesheet"><link href="//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4"><link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222"><meta name="keywords" content="C++,数据结构,数组,"><link rel="alternate" href="/atom.xml" title="云开の博客" type="application/atom+xml"><meta name="description" content="线性表是最基本、最简单、也是最常用的一种数据结构。"><meta property="og:type" content="article"><meta property="og:title" content="C++实现线性表的顺序存储结构"><meta property="og:url" content="https://chenyunxin.cn/posts/3620745992.html"><meta property="og:site_name" content="云开の博客"><meta property="og:description" content="线性表是最基本、最简单、也是最常用的一种数据结构。"><meta property="og:image" content="https://s1.ax1x.com/2020/03/24/8LZYJ1.jpg"><meta property="article:published_time" content="2020-02-20T04:05:20.000Z"><meta property="article:modified_time" content="2020-04-20T05:41:39.007Z"><meta property="article:author" content="云开、见月明"><meta property="article:tag" content="C++"><meta property="article:tag" content="数据结构"><meta property="article:tag" content="数组"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://s1.ax1x.com/2020/03/24/8LZYJ1.jpg"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Pisces",version:"5.1.4",sidebar:{position:"left",display:"post",offset:12,b2t:!1,scrollpercent:!0,onmobile:!0},fancybox:!0,tabs:!0,motion:{enable:!0,async:!1,transition:{post_block:"flipYIn",post_header:"perspectiveRightIn",post_body:"perspectiveLeftIn",coll_header:"perspectiveDownIn",sidebar:"perspectiveUpIn"}},duoshuo:{userId:"0",author:"博主"},algolia:{applicationID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}}}</script><link rel="canonical" href="https://chenyunxin.cn/posts/3620745992.html"><script></script><title>C++实现线性表的顺序存储结构 | 云开の博客</title><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/bubble.js"></script><meta name="generator" content="Hexo 4.2.0"></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans"><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><a href="https://github.com/1999cyx" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#64CEAA;color:#fff;position:absolute;top:0;border:0;right:0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">云开の博客</span> <span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">梦醒繁华，一城荒芜！</h1></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br>首页</a></li><li class="menu-item menu-item-archive"><a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br>归档</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i><br>分类</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i><br>标签</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i><br>关于</a></li><li class="menu-item menu-item-comments"><a href="/comment/" rel="section"><i class="menu-item-icon fa fa-fw fa-comment"></i><br>留言</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="menu-item-icon fa fa-fw fa-link"></i><br>友链</a></li><li class="menu-item menu-item-search"><a href="javascript:;" class="popup-trigger"><i class="menu-item-icon fa fa-search fa-fw"></i><br>搜索</a></li></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav><link href="https://cdn.repository.webfont.com/webfonts/nomal/136790/19356/5e7983c3f629d80b70b700b8.css" rel="stylesheet" type="text/css"></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="https://chenyunxin.cn/posts/3620745992.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="云开、见月明"><meta itemprop="description" content=""><meta itemprop="image" content="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/cloudYun.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="云开の博客"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">C++实现线性表的顺序存储结构</h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-02-20T12:05:20+08:00">2020-02-20 </time></span><span class="post-category"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-folder-o"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span> </a></span></span><span class="post-comments-count"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><a href="/posts/3620745992.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/3620745992.html" itemprop="commentCount"></span> </a></span><span id="/posts/3620745992.html" class="leancloud_visitors" data-flag-title="C++实现线性表的顺序存储结构"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-paw"></i> </span><span class="post-meta-item-text">热度&#58;</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-wordcount"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">字数&#58;</span> <span title="字数">2.7k 字 </span><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span title="阅读时长">11 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><div class="post-gallery" itemscope itemtype="http://schema.org/ImageGallery"><div class="post-gallery-row"><a class="post-gallery-img fancybox" href="https://s1.ax1x.com/2020/03/24/8LZYJ1.jpg" rel="gallery_ck9j80jvn0007qguf6bp8d46i" itemscope itemtype="http://schema.org/ImageObject" itemprop="url"><img src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/loading.gif" data-original="https://s1.ax1x.com/2020/03/24/8LZYJ1.jpg" itemprop="contentUrl"></a></div></div><p><strong>线性表</strong>是最基本、最简单、也是最常用的一种<strong>数据结构</strong>。线性表（<strong>linear list</strong>）是数据结构的一种，一个线性表是n个具有相同特性的数据元素的有限序列。</p><blockquote><p><strong>线性表的特点</strong></p><ul><li>除<strong>第一个元素外</strong>，其他每一个元素<strong>有且仅有一个直接前驱</strong>。</li><li>除<strong>最后一个元素外</strong>，其他每一个元素<strong>有且仅有一个直接后继</strong>。</li><li><strong>直接前驱</strong>和<strong>直接后继</strong>描述了结点之间的<strong>逻辑关系</strong>（即邻接关系）。</li></ul></blockquote><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>顺序表</strong>是以数组的形式保存的<strong>线性表</strong>，将线性表中的元素相继存放在一个<strong>连续的存储空间</strong>中，使得线性表中在<strong>逻辑结构上相邻</strong>的数据元素存储在相邻的物理存储单元中，即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>顺序表的特点</strong>：所有元素的逻辑先后顺序与其物理存放顺序一致。</p><blockquote><p>线性表顺序存储结构的<strong>优缺点</strong>：</p><ul><li><strong>优点</strong>：可以快速的得到表中<strong>任意位置</strong>的元素</li><li><strong>缺点</strong>：1.插入和删除操作需要<strong>移动大量元素</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.当线性表长度变化较大时，<strong>难以确定存储空间容量</strong></li></ul></blockquote><p>顺序表的基本操作：</p><ul><li><p>插入：在<strong>表头</strong>、<strong>表尾</strong>和<strong>第pos个位置</strong>插入数据</p></li><li><p>删除：删除<strong>表头</strong>、<strong>表尾</strong>和<strong>第pos个位置</strong>的数据</p></li><li><p>修改：修改<strong>表头</strong>、<strong>表尾</strong>和<strong>第pos个位置</strong>的数据</p></li><li><p>得到数据：得到<strong>表头</strong>、<strong>表尾</strong>和<strong>第pos个位置</strong>的数据</p></li><li><p>查找：在顺序表中<strong>查找数据p</strong>，返回位置</p></li><li><p>计算长度：返回顺序表的<strong>长度</strong></p></li><li><p>打印</p></li></ul><h2 id="顺序表类的定义"><a href="#顺序表类的定义" class="headerlink" title="顺序表类的定义"></a>顺序表类的定义</h2><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">class</span> <span class="title">SeqList</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    T *data;</span><br><span class="line">    <span class="keyword">int</span> maxSize&#123;&#125;;       <span class="comment">//能存储的最大数据量</span></span><br><span class="line">    <span class="keyword">int</span> last&#123;&#125;;          <span class="comment">//当前存储的元素个数（并非元素下标）</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    SeqList();                               <span class="comment">//构造函数</span></span><br><span class="line">    SeqList(SeqList&lt;T&gt; &amp;<span class="built_in">list</span>);               <span class="comment">//拷贝构造函数</span></span><br><span class="line">    ~SeqList();                              <span class="comment">//析构函数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">reSize</span><span class="params">()</span></span>;                           <span class="comment">//增加空间大小</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">push_first</span><span class="params">(T p)</span></span>;                    <span class="comment">//头插法，在顺序表头插入数据</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">push_last</span><span class="params">(T p)</span></span>;                     <span class="comment">//尾插法，在顺序表尾插入数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">push_pos</span><span class="params">(<span class="keyword">int</span> pos, T p)</span></span>;             <span class="comment">//在顺序表第pos个位置插入数据</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">pop_first</span><span class="params">()</span></span>;                        <span class="comment">//删除顺序表头数据</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">pop_last</span><span class="params">()</span></span>;                         <span class="comment">//删除顺序表尾数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">pop_pos</span><span class="params">(<span class="keyword">int</span> pos)</span></span>;                   <span class="comment">//删除顺序表第pos个数据</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">change_first</span><span class="params">(T p)</span></span>;                  <span class="comment">//修改顺序表头数据</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">change_last</span><span class="params">(T p)</span></span>;                   <span class="comment">//修改顺序表尾数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">change_pos</span><span class="params">(<span class="keyword">int</span> pos, T p)</span></span>;           <span class="comment">//修改顺序表第pos个数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">get_first</span><span class="params">(T &amp;p)</span></span>;                    <span class="comment">//得到顺序表头数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">get_last</span><span class="params">(T &amp;p)</span></span>;                     <span class="comment">//得到顺序表为数据</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">get_pos</span><span class="params">(<span class="keyword">int</span> pos, T &amp;p)</span></span>;             <span class="comment">//得到顺序表第pos个数据</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">search</span><span class="params">(T p)</span></span>;                         <span class="comment">//返回顺序表中p相同的第一个元素的下标pos，若不存在则返回-1</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">isEmpty</span><span class="params">()</span></span>;                          <span class="comment">//判断顺序表是否为空</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>;                              <span class="comment">//返回顺序表能存储的最大数据量</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">length</span><span class="params">()</span></span>;                            <span class="comment">//返回顺序表的长度</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>;                            <span class="comment">//打印顺序表</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span></span>;                            <span class="comment">//清空顺序表</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">delete_link</span><span class="params">()</span></span>;                      <span class="comment">//摧毁顺序表</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></div><h2 id="顺序表中函数的实现"><a href="#顺序表中函数的实现" class="headerlink" title="顺序表中函数的实现"></a>顺序表中函数的实现</h2><h3 id="基本函数（构造函数、析构函数、判空等函数）："><a href="#基本函数（构造函数、析构函数、判空等函数）：" class="headerlink" title="基本函数（构造函数、析构函数、判空等函数）："></a>基本函数（构造函数、析构函数、判空等函数）：</h3><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造函数</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">SeqList</span>&lt;T&gt;:</span>:SeqList() &#123;</span><br><span class="line">    maxSize = <span class="number">50</span>;       <span class="comment">//初始最大存储量为50</span></span><br><span class="line">    last = <span class="number">0</span>;</span><br><span class="line">    data = <span class="keyword">new</span> T[maxSize];   <span class="comment">//给数组分配空间</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (data == <span class="literal">NULL</span>)           <span class="comment">//动态分配失败</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cerr</span> &lt;&lt; <span class="string">"存储分配错误！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//拷贝构造函数</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">SeqList</span>&lt;T&gt;:</span>:SeqList(SeqList&lt;T&gt; &amp;<span class="built_in">list</span>) &#123;</span><br><span class="line">    maxSize = <span class="built_in">list</span>.maxSize;</span><br><span class="line">    last = <span class="built_in">list</span>.last;</span><br><span class="line"></span><br><span class="line">    data = <span class="keyword">new</span> T[maxSize];</span><br><span class="line">    <span class="keyword">if</span> (data == <span class="literal">NULL</span>)           <span class="comment">//动态分配失败</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cerr</span> &lt;&lt; <span class="string">"存储分配错误！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; maxSize; i++)</span><br><span class="line">        data[i] = <span class="built_in">list</span>.data[i];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//析构函数</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">SeqList</span>&lt;T&gt;:</span>:~SeqList() &#123;</span><br><span class="line">    delete_link();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//判断顺序表是否为空</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:isEmpty() &#123;</span><br><span class="line">    <span class="keyword">return</span> last == <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//返回顺序表能存储的最大数据量</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">int</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:size() &#123;</span><br><span class="line">    <span class="keyword">return</span> maxSize;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//清空顺序表</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:clear() &#123;</span><br><span class="line">    <span class="keyword">delete</span> data;</span><br><span class="line">    maxSize = <span class="number">50</span>;</span><br><span class="line">    last = <span class="number">0</span>;</span><br><span class="line">    data = <span class="keyword">new</span> T[maxSize];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//摧毁顺序表</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:delete_link() &#123;</span><br><span class="line">    <span class="keyword">delete</span> data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="打印函数"><a href="#打印函数" class="headerlink" title="打印函数"></a>打印函数</h3><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;打印顺序表中的数据</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//打印顺序表</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:print() &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"线性表数据为： "</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; last - <span class="number">1</span>; i++)</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; data[i] &lt;&lt; <span class="string">" ----&gt; "</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; data[last - <span class="number">1</span>] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="增加空间大小"><a href="#增加空间大小" class="headerlink" title="增加空间大小"></a>增加空间大小</h3><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当顺序表的空间不足时，调用该函数</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//增加空间大小</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:reSize() &#123;</span><br><span class="line">    maxSize += <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    T *temp = <span class="keyword">new</span> T[maxSize];</span><br><span class="line">    <span class="keyword">if</span> (data == <span class="literal">NULL</span>)           <span class="comment">//动态分配失败</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cerr</span> &lt;&lt; <span class="string">"存储分配错误！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; maxSize; i++)</span><br><span class="line">        temp[i] = data[i];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> data;</span><br><span class="line">    data = temp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="插入"><a href="#插入" class="headerlink" title="插入"></a>插入</h3><ul><li><h4 id="头插法"><a href="#头插法" class="headerlink" title="头插法"></a>头插法</h4><blockquote><ol><li>判断顺序表<strong>是否满</strong>了，满了则增加空间大小；</li><li>顺序表中的<strong>所有元素</strong>用for循环进行向<strong>后移动一位</strong>；</li><li>要插入的元素<strong>赋值给数组首元素</strong>；</li><li>顺序表当前<strong>元素个数加1（ last++ )</strong>。</li></ol></blockquote></li><li><h4 id="尾插法"><a href="#尾插法" class="headerlink" title="尾插法"></a>尾插法</h4><blockquote><ol><li>判断顺序表<strong>是否满</strong>了，满了则增加空间大小；</li><li>要插入的元素<strong>赋值给数组第 last 个位置</strong>的元素；</li><li>顺序表当前<strong>元素个数加1（ last++ )</strong>。</li></ol></blockquote></li><li><h4 id="在顺序表的第pos个位置插入数据"><a href="#在顺序表的第pos个位置插入数据" class="headerlink" title="在顺序表的第pos个位置插入数据"></a>在顺序表的第pos个位置插入数据</h4><blockquote><ol><li>判断顺序表是<strong>否满了</strong>，满了则增加空间大小；</li><li>顺序表中的<strong>从第pos-1个位置开始</strong>用for循环进行<strong>向后移动一位</strong>；</li><li>要插入的元素<strong>赋值给数组第 pos-1 个位置</strong>的元素；</li><li>顺序表当前<strong>元素个数加1（ last++ )</strong>。</li></ol></blockquote></li></ul><p>三种插入法代码：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//头插法，在顺序表头插入数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:push_first(T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (last + <span class="number">1</span> &gt; maxSize)    <span class="comment">//空间不足</span></span><br><span class="line">        reSize();</span><br><span class="line">    last++;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = last - <span class="number">1</span>; i &gt; <span class="number">0</span>; i--)   <span class="comment">//全部数据后移一位</span></span><br><span class="line">        data[i] = data[i - <span class="number">1</span>];</span><br><span class="line">    data[<span class="number">0</span>] = p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//尾插法，在顺序表尾插入数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:push_last(T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (last + <span class="number">1</span> &gt; maxSize)    <span class="comment">//空间不足</span></span><br><span class="line">        reSize();</span><br><span class="line">    data[last++] = p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//在顺序表第pos个位置插入数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:push_pos(<span class="keyword">int</span> pos, T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (pos &gt; last + <span class="number">1</span> || pos &lt;= <span class="number">0</span>)      <span class="comment">//不存在第pos个数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (last + <span class="number">1</span> &gt; maxSize)    <span class="comment">//空间不足</span></span><br><span class="line">        reSize();</span><br><span class="line">    last++;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = last - <span class="number">1</span>; i &gt;= pos; i--)   <span class="comment">//第pos个数据后面的数据全部后移一位</span></span><br><span class="line">        data[i] = data[i - <span class="number">1</span>];</span><br><span class="line">    data[pos - <span class="number">1</span>] = p;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><ul><li><h4 id="头删"><a href="#头删" class="headerlink" title="头删"></a>头删</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>顺序表中的<strong>从第 1 个位置的元素</strong>开始用for循环进行向<strong>前移动一位</strong>；</li><li>顺序表当前<strong>元素个数减1（ last - - )</strong>。</li></ol></blockquote></li><li><h4 id="尾删"><a href="#尾删" class="headerlink" title="尾删"></a>尾删</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>顺序表当前<strong>元素个数减1（ last - - )</strong>，覆盖最后一个元素。</li></ol></blockquote></li><li><h4 id="删除第pos个数据"><a href="#删除第pos个数据" class="headerlink" title="删除第pos个数据"></a>删除第pos个数据</h4><blockquote><ol><li><strong>存在第 pos 个数据</strong>（即pos &lt;= last &amp;&amp; pos &gt; 0）；</li><li>顺序表中的<strong>从第 pos-1 个位置的元素</strong>开始用for循环进行向<strong>前移动一位</strong>；</li><li>顺序表当前<strong>元素个数减1（ last - - )</strong>。</li></ol></blockquote></li></ul><p>三种删除法的代码：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除顺序表头数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:pop_first() &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; last - <span class="number">1</span>; i++)       <span class="comment">//从第2个数据开始全部前移一位</span></span><br><span class="line">        data[i] = data[i + <span class="number">1</span>];</span><br><span class="line">    last--;     <span class="comment">//数据个数-1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除顺序表尾数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:pop_last() &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    last--;     <span class="comment">//数据个数-1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除顺序表第pos个数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:pop_pos(<span class="keyword">int</span> pos) &#123;</span><br><span class="line">    <span class="keyword">if</span> (pos &gt; last || pos &lt;= <span class="number">0</span>)      <span class="comment">//不存在第pos个数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = pos - <span class="number">1</span>; i &lt; last - <span class="number">1</span>; i++)       <span class="comment">//从第pos个数据开始全部前移一位</span></span><br><span class="line">        data[i] = data[i + <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">    last--;     <span class="comment">//数据个数-1</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h3><ul><li><h4 id="修改表头数据"><a href="#修改表头数据" class="headerlink" title="修改表头数据"></a>修改表头数据</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>修改表头数据。</li></ol></blockquote></li><li><h4 id="修改表为数据"><a href="#修改表为数据" class="headerlink" title="修改表为数据"></a>修改表为数据</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>修改表尾数据。</li></ol></blockquote></li><li><h4 id="修改第pos个数据"><a href="#修改第pos个数据" class="headerlink" title="修改第pos个数据"></a>修改第pos个数据</h4><blockquote><ol><li><strong>存在第 pos 个数据</strong>（即pos &lt;= last &amp;&amp; pos &gt; 0）；</li><li>修改第pos个数据</li></ol></blockquote></li></ul><p>三种修改法的代码：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//修改顺序表头数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:change_first(T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    data[<span class="number">0</span>] = p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//修改顺序表尾数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:change_last(T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    data[last - <span class="number">1</span>] = p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//修改顺序表第pos个数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:change_pos(<span class="keyword">int</span> pos, T p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (pos &gt; last || pos &lt;= <span class="number">0</span>)      <span class="comment">//不存在第pos个数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    data[pos - <span class="number">1</span>] = p;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="得到数据"><a href="#得到数据" class="headerlink" title="得到数据"></a>得到数据</h3><ul><li><h4 id="得到表头数据"><a href="#得到表头数据" class="headerlink" title="得到表头数据"></a>得到表头数据</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>返回表头数据。</li></ol></blockquote></li><li><h4 id="得到表为数据"><a href="#得到表为数据" class="headerlink" title="得到表为数据"></a>得到表为数据</h4><blockquote><ol><li>顺序表<strong>不为空</strong>；</li><li>返回表尾数据。</li></ol></blockquote></li><li><h4 id="得到第pos个数据"><a href="#得到第pos个数据" class="headerlink" title="得到第pos个数据"></a>得到第pos个数据</h4><blockquote><ol><li><strong>存在第 pos 个数据</strong>（即pos &lt;= last &amp;&amp; pos &gt; 0）；</li><li>返回第pos个数据。</li></ol></blockquote></li></ul><p>三种方法的代码：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//得到顺序表头数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:get_first(T &amp;p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    p = data[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//得到顺序表尾数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:get_last(T &amp;p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (isEmpty())        <span class="comment">//空表直接返回</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    p = data[last - <span class="number">1</span>];</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//得到顺序表第pos个数据</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">bool</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:get_pos(<span class="keyword">int</span> pos, T &amp;p) &#123;</span><br><span class="line">    <span class="keyword">if</span> (pos &gt; last || pos &lt;= <span class="number">0</span>)      <span class="comment">//不存在第pos个数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    p = data[pos - <span class="number">1</span>];</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h3><blockquote><p>返回顺序表中与p<strong>相同的第一个元素</strong>的<strong>下标 pos</strong> ，若<strong>不存在则返回 -1</strong></p></blockquote><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//返回顺序表中p相同的第一个元素的下标pos，若不存在则返回-1</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">int</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:search(T p) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; last; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (data[i] == p)       <span class="comment">//与p相同的第一个元素的下标pos</span></span><br><span class="line">            <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;      <span class="comment">//不存在则返回-1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h3 id="计算长度"><a href="#计算长度" class="headerlink" title="计算长度"></a>计算长度</h3><blockquote><p>返回顺序表的长度</p></blockquote><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="CPP"><figure class="iseeu highlight /cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//返回顺序表的长度</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">int</span> <span class="title">SeqList</span>&lt;T&gt;:</span>:length() &#123;</span><br><span class="line">    <span class="keyword">return</span> last;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><h2 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h2><p><img src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/loading.gif" data-original="https://img-blog.csdnimg.cn/20200215164448340.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0MDM2OTkw,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><img src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/loading.gif" data-original="https://img-blog.csdnimg.cn/20200215164417617.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0MDM2OTkw,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C++线性表的顺序存储结构较链式存储结构简单一些，但是顺序存储结构可能会存在较大的空间浪费和数据移动。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C++顺序表的定义和测试代码都push到github上了，需要的朋友可自行下载：<a href="https://github.com/CYX-code/DataStructure/tree/master/C++/%E7%BA%BF%E6%80%A7%E8%A1%A8%E7%9A%84%E9%A1%BA%E5%BA%8F%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84" target="_blank" rel="noopener">C++线性表的顺序存储结构</a>。</p></div><div class="popular-posts-header"><i class="fa fa-graduation-cap"></i> 相关文章</div><details><summary>点击查看</summary><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3058917998.html" rel="bookmark">C++实现链式队列和线性队列</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3345531452.html" rel="bookmark">C++实现链式栈和线性栈</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\644523100.html" rel="bookmark">C++实现线性表的链式存储结构（循环链表）</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\382057249.html" rel="bookmark">C++实现线性表的链式存储结构（单链表）</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\198006710.html" rel="bookmark">栈的应用</a></div></li></ul></details><div><div class="my_post_copyright"><script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script><script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script><script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script><p><span>本文标题:</span><a href="/posts/3620745992.html">C++实现线性表的顺序存储结构</a></p><p><span>文章作者:</span><a href="/" title="访问 云开、见月明 的个人博客">云开、见月明</a></p><p><span>发布时间:</span>2020年02月20日 - 12:05:20</p><p><span>最后更新:</span>2020年04月20日 - 13:41:39</p><p><span>原始链接:</span><a href="/posts/3620745992.html" title="C++实现线性表的顺序存储结构">https://chenyunxin.cn/posts/3620745992.html</a> <span class="copy-path" title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://chenyunxin.cn/posts/3620745992.html" aria-label="复制成功！"></i></span></p><p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p></div><script>var clipboard=new Clipboard(".fa-clipboard");$(".fa-clipboard").click(function(){clipboard.on("success",function(){swal({title:"",text:"复制成功",icon:"success",showConfirmButton:!0})})})</script></div><div><div><div class="read-over">-------------------本文结束 <i class="fa fa-paw"></i> 感谢您的阅读-------------------</div></div></div><div><div class="share_reward"><div>坚持原创技术分享，感谢您的支持和鼓励！</div><button id="rewardButton" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span>打赏</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/loading.gif" data-original="/images/wechatpay.jpg" alt="云开、见月明 微信支付"><p>微信支付</p></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/loading.gif" data-original="/images/alipay.jpg" alt="云开、见月明 支付宝"><p>支付宝</p></div></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/C/" rel="tag"><i class="fa fa-tag"></i> C++</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"><i class="fa fa-tag"></i> 数据结构</a> <a href="/tags/%E6%95%B0%E7%BB%84/" rel="tag"><i class="fa fa-tag"></i> 数组</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/posts/382057249.html" rel="prev" title="C++实现线性表的链式存储结构（单链表）">C++实现线性表的链式存储结构（单链表） <i class="fa fa-chevron-right"></i></a></div></div></footer></div></article><div class="post-spread"><div data-weibo-title="分享到微博" data-qq-title="分享到QQ" data-douban-title="分享到豆瓣" class="social-share" class="share-component" data-disabled="qzone,google+,linkedin" data-description="Share.js - 一键分享到微博，QQ空间，腾讯微博，人人，豆瓣...">分享到：</div></div></div></div><div class="comments" id="comments"></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div id="sidebar-dimmer"></div><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap">站点概览</li></ul><section class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><a href="/"><img class="site-author-image" itemprop="image" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/images/cloudYun.jpg" alt="云开、见月明"></a><p class="site-author-name" itemprop="name">云开、见月明</p><p class="site-description motion-element" itemprop="description"></p></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"><a href="/archives"><span class="site-state-item-count">12</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/index.html"><span class="site-state-item-count">8</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/index.html"><span class="site-state-item-count">13</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="feed-link motion-element"><a href="/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS </a><a title="收藏到书签，偶尔High一下^_^" rel="alternate" class="mw-harlem_shake_slow wobble shake" href="javascript:void(0)" onclick="(    /*     * Copyright (C) 2015 Rocko (rocko.xyz) <rocko.zxp@gmail.com>     *     * Licensed under the Apache License, Version 2.0 (the 'License');     * you may not use this file except in compliance with the License.     * You may obtain a copy of the License at     *     *      http://www.apache.org/licenses/LICENSE-2.0     *     * Unless required by applicable law or agreed to in writing, software     * distributed under the License is distributed on an 'AS IS' BASIS,     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     * See the License for the specific language governing permissions and     * limitations under the License.     */    function go() {        function c() {        var e = document.createElement('link');        e.setAttribute('type', 'text/css');        e.setAttribute('rel', 'stylesheet');        e.setAttribute('href', f);        e.setAttribute('class', l);        document.body.appendChild(e)    }     function h() {        var e = document.getElementsByClassName(l);        for (var t = 0; t < e.length; t++) {            document.body.removeChild(e[t])        }    }     function p() {        var e = document.createElement('div');        e.setAttribute('class', a);        document.body.appendChild(e);        setTimeout(function() {            document.body.removeChild(e)        }, 100)    }     function d(e) {        return {            height : e.offsetHeight,            width : e.offsetWidth        }    }     function v(i) {        var s = d(i);        return s.height > e &amp;&amp; s.height < n &amp;&amp; s.width > t &amp;&amp; s.width < r    }     function m(e) {        var t = e;        var n = 0;        while (!!t) {            n += t.offsetTop;            t = t.offsetParent        }        return n    }     function g() {        var e = document.documentElement;        if (!!window.innerWidth) {            return window.innerHeight        } else if (e &amp;&amp; !isNaN(e.clientHeight)) {            return e.clientHeight        }        return 0    }     function y() {        if (window.pageYOffset) {            return window.pageYOffset        }        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)    }     function E(e) {        var t = m(e);        return t >= w &amp;&amp; t <= b + w    }     var songs = [                'http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3', 'http://www.ytmp3.cn/down/57563.mp3'  ];    function S() {        var e = document.getElementById('audio_element_id');        if(e != null){            var index = parseInt(e.getAttribute('curSongIndex'));            if(index > songs.length - 2) {                index = 0;            } else {                index++;            }            e.setAttribute('curSongIndex', index);            N();        }        e.src = i;        e.play()    }     function x(e) {        e.className += ' ' + s + ' ' + o    }     function T(e) {        e.className += ' ' + s + ' ' + u[Math.floor(Math.random() * u.length)]    }     function N() {        var e = document.getElementsByClassName(s);        var t = new RegExp('\\b' + s + '\\b');        for (var n = 0; n < e.length; ) {            e[n].className = e[n].className.replace(t, '')        }    }    function initAudioEle() {        var e = document.getElementById('audio_element_id');        if(e === null){            e = document.createElement('audio');            e.setAttribute('class', l);            e.setAttribute('curSongIndex', 0);            e.id = 'audio_element_id';            e.loop = false;            e.bgcolor = 0;            e.addEventListener('canplay', function() {            setTimeout(function() {                x(k)            }, 500);            setTimeout(function() {                N();                p();                for (var e = 0; e < O.length; e++) {                    T(O[e])                }            }, 15500)        }, true);        e.addEventListener('ended', function() {            N();            h();            go();        }, true);        e.innerHTML = ' <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>';        document.body.appendChild(e);        }    }        initAudioEle();    var e = 30;    var t = 30;    var n = 350;    var r = 350;    var curSongIndex = parseInt(document.getElementById('audio_element_id').getAttribute('curSongIndex'));    var i = songs[curSongIndex];        var s = 'mw-harlem_shake_me';    var o = 'im_first';    var u = ['im_drunk', 'im_baked', 'im_trippin', 'im_blown'];    var a = 'mw-strobe_light';    var f = 'https://rocko.xyz/css/harlem-shake-style.css';        var l = 'mw_added_css';    var b = g();    var w = y();    var C = document.getElementsByTagName('*');    var k = null;    for (var L = 0; L < C.length; L++) {        var A = C[L];        if (v(A)) {            if (E(A)) {                k = A;                break            }        }    }    if (A === null) {        console.warn('Could not find a node of the right size. Please try a different page.');        return    }    c();    S();    var O = [];    for (var L = 0; L < C.length; L++) {        var A = C[L];        if (v(A)) {            O.push(A)        }    }    })()"><i class="fa fa-music"></i> High~</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a rel="external nofollow" href="mailto:1755899681@qq.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a> </span><span class="links-of-author-item"><a rel="external nofollow" href="https://github.com/1999cyx" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a rel="external nofollow" href="https://blog.csdn.net/qq_44036990" target="_blank" title="CSDN"><i class="fa fa-fw fa-copyright"></i>CSDN</a></span></div><div class="links-of-blogroll motion-element links-of-blogroll-block"><div class="links-of-blogroll-title"><i class="fa fa-history fa-" aria-hidden="true"></i> 近期文章</div><ul class="links-of-blogroll-list"><li class="my-links-of-blogroll-li"><a href="/posts/1736494633.html" title="hexo(sakura)——标签、分类页面美化" target="_blank">hexo(sakura)——标签、分类页面美化</a></li><li class="my-links-of-blogroll-li"><a href="/posts/1779179582.html" title="Github+PicGo配置私人图床踩坑记" target="_blank">Github+PicGo配置私人图床踩坑记</a></li><li class="my-links-of-blogroll-li"><a href="/posts/3058917998.html" title="C++实现链式队列和线性队列" target="_blank">C++实现链式队列和线性队列</a></li><li class="my-links-of-blogroll-li"><a href="/posts/198006710.html" title="栈的应用" target="_blank">栈的应用</a></li><li class="my-links-of-blogroll-li"><a href="/posts/4200755795.html" title="hexo(next)——每日一言、今日诗词" target="_blank">hexo(next)——每日一言、今日诗词</a></li></ul></div><div><canvas id="canvas" style="width:60%"></canvas></div><script>!function(){function t(t){var r=[];a.fillStyle="#005eac";var h=new Date,u=70,s=30,v=h.getHours(),g=Math.floor(v/10),m=v%10;r.push({num:g}),r.push({num:m}),r.push({num:10});var c=h.getMinutes(),g=Math.floor(c/10),m=c%10;r.push({num:g}),r.push({num:m}),r.push({num:10});var M=h.getSeconds(),g=Math.floor(M/10),m=M%10;r.push({num:g}),r.push({num:m});for(var p=0;p<r.length;p++)r[p].offsetX=u,u=f(u,s,r[p].num,t),p<r.length-1&&10!=r[p].num&&10!=r[p+1].num&&(u+=l);if(0==i.length)i=r;else for(var C=0;C<i.length;C++)i[C].num!=r[C].num&&(n(r[C]),i[C].num=r[C].num);return e(t),o(),h}function n(t){for(var n=t.num,e=m[n],o=0;o<e.length;o++)for(var f=0;f<e[o].length;f++)if(1==e[o][f]){var a={offsetX:t.offsetX+u+2*u*f,offsetY:30+u+2*u*o,color:g[Math.floor(Math.random()*g.length)],g:1.5+Math.random(),vx:4*Math.pow(-1,Math.ceil(10*Math.random()))+Math.random(),vy:-5};v.push(a)}}function e(t){for(var n=0;n<v.length;n++)t.beginPath(),t.fillStyle=v[n].color,t.arc(v[n].offsetX,v[n].offsetY,u,0,2*Math.PI),t.fill()}function o(){for(var t=0,n=0;n<v.length;n++){var e=v[n];e.offsetX+=e.vx,e.offsetY+=e.vy,e.vy+=e.g,e.offsetY>h-u&&(e.offsetY=h-u,e.vy=-e.vy*s),e.offsetX>u&&e.offsetX<r-u&&(v[t]=v[n],t++)}for(;t<v.length;t++)v.pop()}function f(t,n,e,o){for(var f=m[e],a=0;a<f.length;a++)for(var r=0;r<f[a].length;r++)1==f[a][r]&&(o.beginPath(),o.arc(t+u+2*u*r,n+u+2*u*a,u,0,2*Math.PI),o.fill());return o.beginPath(),t+=f[0].length*u*2}var a,r=820,h=250,u=7,l=10,s=.65,v=[];const g=["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"];var i=[],m=[[[0,0,1,1,1,0,0],[0,1,1,0,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,0,1,1,0],[0,0,1,1,1,0,0]],[[0,0,0,1,1,0,0],[0,1,1,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[1,1,1,1,1,1,1]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,1,1,0,0,0],[0,1,1,0,0,0,0],[1,1,0,0,0,0,0],[1,1,0,0,0,1,1],[1,1,1,1,1,1,1]],[[1,1,1,1,1,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,1,1,1,0,0],[0,0,0,0,1,1,0],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,0,0,0,1,1,0],[0,0,0,1,1,1,0],[0,0,1,1,1,1,0],[0,1,1,0,1,1,0],[1,1,0,0,1,1,0],[1,1,1,1,1,1,1],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,1,1,1,1]],[[1,1,1,1,1,1,1],[1,1,0,0,0,0,0],[1,1,0,0,0,0,0],[1,1,1,1,1,1,0],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,0,0,0,1,1,0],[0,0,1,1,0,0,0],[0,1,1,0,0,0,0],[1,1,0,0,0,0,0],[1,1,0,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[1,1,1,1,1,1,1],[1,1,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,1,1,0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,0,0,0]]],c=document.getElementById("canvas");c.width=r,c.height=h,a=c.getContext("2d"),new Date,setInterval(function(){a.clearRect(0,0,a.canvas.width,a.canvas.height),t(a)},50)}()</script><div id="days"></div><script language="javascript">function show_date_time(){window.setTimeout("show_date_time()",1e3),BirthDay=new Date("02/20/2020 00:00:00"),today=new Date,timeold=today.getTime()-BirthDay.getTime(),sectimeold=timeold/1e3,secondsold=Math.floor(sectimeold),msPerDay=864e5,e_daysold=timeold/msPerDay,daysold=Math.floor(e_daysold),e_hrsold=24*(e_daysold-daysold),hrsold=setzero(Math.floor(e_hrsold)),e_minsold=60*(e_hrsold-hrsold),minsold=setzero(Math.floor(60*(e_hrsold-hrsold))),seconds=setzero(Math.floor(60*(e_minsold-minsold))),document.getElementById("days").innerHTML="已运行"+daysold+"天"+hrsold+"时"+minsold+"分"+seconds+"秒"}function setzero(e){return e<10&&(e="0"+e),e}show_date_time()</script></div></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#顺序表类的定义"><span class="nav-number">1.</span> <span class="nav-text">顺序表类的定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#顺序表中函数的实现"><span class="nav-number">2.</span> <span class="nav-text">顺序表中函数的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本函数（构造函数、析构函数、判空等函数）："><span class="nav-number">2.1.</span> <span class="nav-text">基本函数（构造函数、析构函数、判空等函数）：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#打印函数"><span class="nav-number">2.2.</span> <span class="nav-text">打印函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#增加空间大小"><span class="nav-number">2.3.</span> <span class="nav-text">增加空间大小</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#插入"><span class="nav-number">2.4.</span> <span class="nav-text">插入</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#头插法"><span class="nav-number">2.4.1.</span> <span class="nav-text">头插法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#尾插法"><span class="nav-number">2.4.2.</span> <span class="nav-text">尾插法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#在顺序表的第pos个位置插入数据"><span class="nav-number">2.4.3.</span> <span class="nav-text">在顺序表的第pos个位置插入数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除"><span class="nav-number">2.5.</span> <span class="nav-text">删除</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#头删"><span class="nav-number">2.5.1.</span> <span class="nav-text">头删</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#尾删"><span class="nav-number">2.5.2.</span> <span class="nav-text">尾删</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除第pos个数据"><span class="nav-number">2.5.3.</span> <span class="nav-text">删除第pos个数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改"><span class="nav-number">2.6.</span> <span class="nav-text">修改</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#修改表头数据"><span class="nav-number">2.6.1.</span> <span class="nav-text">修改表头数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改表为数据"><span class="nav-number">2.6.2.</span> <span class="nav-text">修改表为数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改第pos个数据"><span class="nav-number">2.6.3.</span> <span class="nav-text">修改第pos个数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#得到数据"><span class="nav-number">2.7.</span> <span class="nav-text">得到数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#得到表头数据"><span class="nav-number">2.7.1.</span> <span class="nav-text">得到表头数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#得到表为数据"><span class="nav-number">2.7.2.</span> <span class="nav-text">得到表为数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#得到第pos个数据"><span class="nav-number">2.7.3.</span> <span class="nav-text">得到第pos个数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查找"><span class="nav-number">2.8.</span> <span class="nav-text">查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#计算长度"><span class="nav-number">2.9.</span> <span class="nav-text">计算长度</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运行结果"><span class="nav-number">3.</span> <span class="nav-text">运行结果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结"><span class="nav-number">4.</span> <span class="nav-text">小结</span></a></li></ol></div></div></section></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heartbeat"></i> </span><span class="author" itemprop="copyrightHolder">云开、见月明</span></div><div class="theme-info"><div class="powered-by"></div><span class="post-count">全站共21.8k字 &nbsp;|&nbsp; Since 2020/02/20 ❥(^_-)</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="site-uv">我的第 <span class="busuanzi-value" id="busuanzi_value_site_uv"></span> 位朋友 </span><span class="site-pv">经历 <span class="busuanzi-value" id="busuanzi_value_site_pv"></span> 次回眸才与你相遇</span></div></div></footer><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span id="scrollpercent"><span>0</span>%</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/velocity-animate@1.2.1/velocity.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/npm/velocity-animate@1.2.1/velocity.ui.min.js"></script><script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="/js/src/Valine.min.js"></script><style>.v .vlist{border-radius:10px;padding-top:10px}.v .vwrap{border:2px solid #1acddb;border-radius:10px;padding:0 0 44px}.v .veditor{min-height:10rem;background-image:url(https://s1.ax1x.com/2020/03/24/8LLItH.png);background-size:contain;background-repeat:no-repeat;background-position:left;background-color:rgba(255,255,255,0);resize:none}.v .vwrap .vedit{padding-top:20px}.v .vwrap .vheader{width:80%;bottom:0;position:absolute;background:#fff7f7f7}.v .vinput{padding:10px 15px;text-align:center}.v .vwrap .vheader .vinput{border-bottom:0}.v .vwrap .vedit .vctrl{margin-top:-44px;right:0;position:absolute;margin-right:-3px}.v .vwrap .vcontrol{position:absolute;right:0;bottom:0;width:20%;padding-top:0}.v .vwrap .vcontrol .col.col-20,.v .vwrap .vedit .vctrl span.vpreview-btn{display:none}.v .vwrap .vcontrol .col.col-80{width:100%}.v .vbtn.vsubmit{border-radius:10px;padding:0;color:#fff;line-height:44px;width:100%;border:none;background:#f99}.v .vbtn.vsubmit:hover{border-radius:10px;padding:0;color:#fff;line-height:44px;width:100%;border:none;background:#c3dfe3}.v .vlist .vcard .vhead .vsys{background:#91f6d6}@media screen and (max-width:520px){.v .vwrap .vheader .vinput{width:33.33%;padding:10px 5px}}a.at{font-size:20px;color:#bea124}</style><script type="text/javascript">new Valine({lang:"zh-cn",admin_email:"1755899681@qq.com",el:"#comments",appId:"47oVoB4SBf9W5vXsRcqzLw2y-gzGzoHsz",appKey:"03rXHWk23vFQQasnSaFrYOa4",placeholder:"是我一生只会遇见一次的惊喜 ..."}),$(document).ready(function(){$("#comments").on("click","span.vat",function(){$(this).parent("div.vmeta").next("div.vcontent").after($("div.vwrap")),$("textarea#veditor").focus()})})</script><script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script><script type="text/javascript">jinrishici.load(function(t){var i=t.data.content+"\n「"+t.data.origin.title+"」"+t.data.origin.dynasty+" · "+t.data.origin.author;document.getElementById("veditor").setAttribute("placeholder",i)})</script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="./public/search.xml";0===search_path.length?search_path="search.xml":/json$/i.test(search_path)&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var $=[];0!=g.length;){var C=g[g.length-1],m=C.position,x=C.word,w=m-20,y=m+80;w<0&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),$.push(r(p,w,y,g))}$.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&($=$.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",$.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script><script>function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log('Failed to save Visitor num, with error message: ' + responseJSON.error);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log('LeanCloud Counter Error: ' + responseJSON.code + ' ' + responseJSON.error);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + '47oVoB4SBf9W5vXsRcqzLw2y-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': '47oVoB4SBf9W5vXsRcqzLw2y-gzGzoHsz',
                'X-LC-Key': '03rXHWk23vFQQasnSaFrYOa4',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
            if (localhost.test(document.URL)) return;
            addCount(Counter);
          
        });
    });</script><script>!function(){var t=document.createElement("script"),s=window.location.protocol.split(":")[0];"https"===s?t.src="https://zz.bdstatic.com/linksubmit/push.js":t.src="http://push.zhanzhang.baidu.com/push.js";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}()</script><script src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/cursor/love.min.js"></script><script src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE)</script><script async src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/sakura.js"></script><script async src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/fairyDustCursor.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/clipboard-use.js"></script><link rel="stylesheet" href="/sharejs/css/share.min.css"><script src="/sharejs/js/social-share.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/1999cyx/cdn@1.0/js/src/linkcard.js"></script><script>!function(t){function n(){for(var n=0;n<e.length;n++)i=e[n],0<=(o=i.getBoundingClientRect()).bottom&&0<=o.left&&o.top<=(t.innerHeight||document.documentElement.clientHeight)&&function(){var t,i,o,r,c=e[n];t=c,i=function(){e=e.filter(function(t){return c!==t})},o=new Image,r=t.getAttribute("data-original"),o.onload=function(){t.src=r,i&&i()},o.src=r}();var i,o}var e=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));n(),t.addEventListener("scroll",function(){var e,i;e=n,i=t,clearTimeout(e.tId),e.tId=setTimeout(function(){e.call(i)},500)})}(this)</script><script>window.addEventListener("load",function(){var a=/\.(gif|jpg|jpeg|tiff|png)$/i,e=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(t){var r=t.parentNode;"A"===r.tagName&&(r.href.match(a)||r.href.match(e))&&(r.href=t.dataset.original)})})</script></body></html><!-- rebuild by neat -->